<?php

/*************************************************************************

	FILE INCLUDE

**************************************************************************/
include_once '../lib/Template.class.php';
$debug_mode_value = 0;
checkLogin();

sajax_init();
$sajax_debug_mode = 0;
sajax_export("delCandidate");
sajax_export("changeRepresent");
sajax_export("setNer");
sajax_export("getCandidate");
sajax_export("changeStatus");
sajax_export("getNaverData");
sajax_handle_client_request();
/*************************************************************************

	USER SCRIPT

***************************************************************************/
$userscript =<<<js
    <script type="text/javascript">
        <!--

			function checkWiki(){
				if(confirm("Are you sure you want to update it?")){
					document.location.replace("./WikiCheck.php");
				}
			}

        	function getCSV(no){
				obj=document.getElementById("searchForm")
				obj.action="./csvdown"+no+".php";
				obj.submit();
				obj.action="./SynonymSearch.php";
        	}

	        function getNaverData(){
	        	keword = document.getElementById("naver_keyword").value;
	        	if(keword.length > 0){
						x_getNaverData( keword, setNaverData );
					}else{
						alert("input keyword");
					}
	        }
	        function setNaverData(result){
				htm1="";
				htm2="";
				json = eval('(' + result + ')');
				htm1+="<span class='onTitle'>News</span> <span class='offTitle' onMouseOver='changeNaverResult(2);'>Blog</span>";
				htm1+="<div class='cnt'>News 검색 결과 총 "+json.news.channel.total+"건</div>";
				htm1+="<ul>";
				for(i=0;i<json.news.channel.item.length;i++){
					reg=json.news.channel.item[i].pubDate.split(" ");
					htm1+="<li class='tit'><a href='"+json.news.channel.item[i].originallink+"' target='_blank'>"+json.news.channel.item[i].title+"</a></li>";
					htm1+="<li class='cont'>"+json.news.channel.item[i].description+"<span>&nbsp; - "+reg[1]+"."+reg[2]+"."+reg[3]+"</span></li>";
				}
				htm1+="</ul>";
				document.getElementById("searchbox_news").innerHTML=htm1;

				htm2+="<span class='offTitle' onMouseOver='changeNaverResult(1);'>News</span> <span class='onTitle'>Blog</span>";
				htm2+="<div class='cnt'>Blog 검색 결과 총 "+json.blog.channel.total+"건</div>";
				htm2+="<ul>";
				for(i=0;i<json.blog.channel.item.length;i++){
					htm2+="<li class='tit'><a href='"+json.blog.channel.item[i].link+"' target='_blank'>"+json.blog.channel.item[i].title+"</a></li>";
					htm2+="<li class='cont'>"+json.blog.channel.item[i].description+"<span>&nbsp; - "+json.blog.channel.item[i].bloggername+"</span></li>";
				}
				htm2+="</ul>";
				document.getElementById("searchbox_blog").innerHTML=htm2;

	        }

			function delCandidate(cluster_cd){
				if(confirm("Are you sure you want to delete it?")){
					name="chk_"+cluster_cd;
					obj=document.getElementsByName(name);
					for(i=0;i<obj.length;i++){
						if(obj[i].checked){
							cadidate_cd = obj[i].value;
							x_delCandidate( cadidate_cd,cluster_cd, reloadCandidate );
						}
					}
				}
			}
			function reloadCandidate(result){
				getCandidate(result);
			}
            function changeRepresent(cluster_cd){
            	name="chk_"+cluster_cd;
				obj=document.getElementsByName(name);
				for(i=0;i<obj.length;i++){
					if(obj[i].checked){
						cadidate_cd = obj[i].value;
						x_changeRepresent( cadidate_cd,cluster_cd, reloadCandidate );
					}
				}
            }
			function setNer(ner_cd,cluster_cd){
				x_setNer( ner_cd, cluster_cd, dummyFun );
			}
			function getCandidate(cluster_cd){
				x_getCandidate( cluster_cd, setCandidate );
			}

			function setCandidate(result){
				var htm="";
				var represent=""
				for(var i in result){
						htm += "<input name='chk_"+result[i].CLUSTER_CD+"' type='checkbox' value='"+result[i].CANDIDATE_CD+"'> "+result[i].CANDIDATE_NM+"&nbsp;&nbsp;&nbsp;&nbsp;"
						cluster_cd = result[i].CLUSTER_CD;
						if(result[i].REPRESENT_FL == 1){
							represent = result[i].CANDIDATE_NM;
						}
					}
				name1 = "cadidate_td_"+cluster_cd;
				name2 = "represent_td_"+cluster_cd;
				document.getElementById(name1).innerHTML=htm;
				document.getElementById(name2).innerHTML=represent;
			}

			function changeStatus(cluster_cd,cluster_st){
				if(cluster_st==4){
					name = "question_"+cluster_cd;
					msg = document.getElementById(name).value;
					if(msg.length > 0){
						x_changeStatus( cluster_cd, cluster_st, msg, reloadStatus );
					}else{
						alert("input message");
					}
				}else if(cluster_st==5){
					name = "answer_"+cluster_cd;
					msg = document.getElementById(name).value;
					if(msg.length > 0){
						x_changeStatus( cluster_cd, cluster_st, msg, reloadStatus );
					}else{
						alert("input message");
					}
				}else{
					x_changeStatus( cluster_cd, cluster_st, reloadStatus );
				}

			}

			function reloadStatus(result){
				var rt = result.split(",");
				var htm="";

				name1="status_td_"+rt[0];
				name2="operation2_td_"+rt[0];
				cname="status_"+rt[1];

				if(rt[1]==1){
					sname = "waiting";
				}else if(rt[1]==2){
					sname = "skip";
				}else if(rt[1]==3){
					sname = "save";
				}else if(rt[1]==4){
					sname = "request";
				}else if(rt[1]==5){
					sname = "answer";
				}else if(rt[1]==6){
					sname = "checked";
				}
				document.getElementById(name1).className=cname;
				document.getElementById(name1).innerHTML=sname;

				if(rt[1]!=1){
					htm+="<input class='button' type='button' value='RETURN' onClick='changeStatus("+rt[0]+",1)'>";
					document.getElementById(name2).innerHTML=htm;
				}else{
					htm+="<input class='button' type='button' value='REQUEST' onClick='changeStatus("+rt[0]+",4)'> "+
						"<input class='button' type='button' value='SKIP' onClick='changeStatus("+rt[0]+",2)'> "+
						"<input class='button' type='button' value='SAVE' onClick='changeStatus("+rt[0]+",3)'>";
					document.getElementById(name2).innerHTML=htm;
				}
			}
			function dummyFun(){

			}
        //-->
    </script>
js;


/*************************************************************************

		PAGE MAIN PROCESS

***************************************************************************/
(!empty($_REQUEST['page']))? $page = $_REQUEST['page'] : $page = 1;
(!empty($_REQUEST['search_ner']))? $search_ner = $_REQUEST['search_ner'] : $search_ner = "";
(!empty($_REQUEST['search_domain']))? $search_domain = $_REQUEST['search_domain'] : $search_domain = "";
(!empty($_REQUEST['search_status']))? $search_status = $_REQUEST['search_status'] : $search_status = "";
(!empty($_REQUEST['search_cluster_1']))? $search_cluster_1 = $_REQUEST['search_cluster_1'] : $search_cluster_1 = "";
(!empty($_REQUEST['search_cluster_2']))? $search_cluster_2 = $_REQUEST['search_cluster_2'] : $search_cluster_2 = "";
(!empty($_REQUEST['search_candidate']))? $search_candidate = htmlspecialchars($_REQUEST['search_candidate']) : $search_candidate = "";

$query_add="";

if(!empty($search_ner)){
	$query_add .= "AND a.NER_CD =  $search_ner ";
	$link_add .= "&search_ner=$search_ner";
}
if(!empty($search_domain)){
	$query_add .= "AND a.DOMAIN_CD = $search_domain ";
	$link_add .= "&search_domain=$search_domain";
}
if(!empty($search_cluster_1)){
	$query_add .= "AND a.CLUSTER_CD >=  $search_cluster_1 ";
	$link_add .= "&search_cluster_1=$search_cluster_1";
}
if(!empty($search_cluster_2)){
	$query_add .= "AND a.CLUSTER_CD <=  $search_cluster_2 ";
	$link_add .= "&search_cluster_2=$search_cluster_2";
}
if(!empty($search_candidate)){
	$query_add .= "AND a.CLUSTER_CD in (SELECT CLUSTER_CD FROM candidate_tb WHERE CANDIDATE_NM='$search_candidate')";
	$link_add .= "&search_candidate=$search_candidate";
}
if(!empty($search_status)){
	$query_add .= "AND a.CLUSTER_ST = $search_status ";
	$link_add .= "&search_status=$search_status";
}

$db->conn();
$sql = "select count(*)as cnt from cluster_tb a where merging_st =0 AND a.CLUSTER_ST >= 6 $query_add";
$c_re=$db->fetch($sql);
$totalrow = $c_re["cnt"];

if(!empty($totalrow)){
	$arr['total']= $totalrow;
	$arr['page'] = $page;
	$arr['row']=15;
	$arr['scale']=10;
	$arr['link']=$link_add;
	$paging= $db->paging($arr);

	$sql = "select a.*,b.CANDIDATE_CD, b.CANDIDATE_NM, c.NER_NM
			from cluster_tb a
				left outer join candidate_tb b
				on a.CLUSTER_CD = b.CLUSTER_CD
				and b.REPRESENT_FL = 1
					left outer join ner_tb c
				    on a.NER_CD = c.NER_CD
			where merging_st =0 AND a.CLUSTER_ST >= 6  $query_add
			order by cluster_cd desc
			limit ".$paging["limit"]." offset ".$paging["offset"]." ";
	$rs = $db->query($sql);
	$num = $db->num($rs);
	$db->selectTotal = $num;
	$list = array();
	if( $num > 0 )
	{
		for( $i = 0; $i < $num; $i++)
		{
			$list[$i] = $db->fetch_assoc($rs);
			$cluster_cd=$list[$i][CLUSTER_CD];
			$list[$i][candidate] = $db->select("select CANDIDATE_CD, CANDIDATE_NM from candidate_tb where cluster_cd=$cluster_cd AND REPRESENT_FL <> 1 ");
		}
	}
	else
	{
		$list = "";
	}

}

$db->close();


$db->conn();
$select_opt = $db->get_selectbox();
$db->close();

print_rr($select_opt);
/*************************************************************************

	TEMPLATE PROCESS

***************************************************************************/
$body = "SynonymSearch.html";

$tpl = new Template();
$tpl->setDefine($body);

$tpl->assign("list", $list);
$tpl->assign("paging", $paging);
$tpl->assign("select_opt",$select_opt);

$tpl->assign("search_ner",$search_ner);
$tpl->assign("search_domain",$search_domain);
$tpl->assign("search_status",$search_status);
$tpl->assign("search_cluster_1",$search_cluster_1);
$tpl->assign("search_cluster_2",$search_cluster_2);
$tpl->assign("search_candidate",$search_candidate);

$tpl->setPrint();



/*************************************************************************

	AJAX FUNCTION : Template_ function

***************************************************************************/
function getNaverData($keword){
	$keword = urlencode(Unescape($keword));
	$url = "http://openapi.naver.com/search?query=$keword&target=news&key=b61a30ae11cfd7d10e017250717a3213";//";//b61a30ae11cfd7d10e017250717a3213
	$result = simplexml_load_file($url);
	$json1 = json_encode($result);
	$url = "http://openapi.naver.com/search?query=$keword&target=blog&key=b61a30ae11cfd7d10e017250717a3213";//";//b61a30ae11cfd7d10e017250717a3213
	$result = simplexml_load_file($url);
	$json2 = json_encode($result);

	$temp["news"] = json_decode($json1);
	$temp["blog"] = json_decode($json2);
	$json = json_encode($temp);

	return $json;
}

function delCandidate($candidate_cd,$cluster_cd){
	global $db;

	$db->conn();
	$rt=$db->fetch("select candidate_nm from candidate_tb where CANDIDATE_CD = $candidate_cd");
	$db->query("INSERT INTO candidate_history_tb
				(CANDIDATE_CD, OPERATION, USER_CD, REGISTER_DT,ETC)
				VALUES ($candidate_cd, 9, $_SESSION[user_cd], now(),'$rt[candidate_nm]')");
	$db->query("DELETE FROM candidate_tb WHERE CANDIDATE_CD = $candidate_cd");
	$db->close();

	return $cluster_cd;
}

function changeRepresent($candidate_cd,$cluster_cd){
	global $db;

	$db->conn();
	$db->query("UPDATE candidate_tb SET REPRESENT_FL = 0	WHERE CLUSTER_CD = $cluster_cd");
	$db->query("UPDATE candidate_tb SET REPRESENT_FL = 1	WHERE CANDIDATE_CD = $candidate_cd");
	$db->close();

	return $cluster_cd;
}

function setNer($ner_cd,$cluster_cd){
	global $db;

	$db->conn();
	$db->query("UPDATE cluster_tb SET NER_CD = $ner_cd	WHERE CLUSTER_CD = $cluster_cd");
	$db->close();

}
function getCandidate($cluster_cd){
	global $db;

	$db->conn();
	$list = $db->select("select CLUSTER_CD, CANDIDATE_CD, CANDIDATE_NM, REPRESENT_FL from candidate_tb where cluster_cd=$cluster_cd");
	$db->close();
	return $list;
}
function changeStatus($cluster_cd, $cluster_st, $msg=""){
	global $db;

	$db->conn();
	if ($cluster_st==4){
		$msg = Unescape($msg);
		$db->query("UPDATE comment_tb SET LASTEST_FL = 0 WHERE CLUSTER_CD = $cluster_cd");
		$db->query("INSERT INTO comment_tb
					(CLUSTER_CD, COMMENT, COMMENT_DT, COMMENT_USER, LASTEST_FL)
					VALUES ( $cluster_cd, '$msg', now(), $_SESSION[user_cd], 1)");
	}
	if ($cluster_st==5){
		$msg = Unescape($msg);
		$db->query("UPDATE comment_tb SET ANSWER = '$msg',ANSWER_DT = now(),ANSWER_USER =$_SESSION[user_cd]  WHERE CLUSTER_CD = $cluster_cd AND LASTEST_FL =1" );
	}
	$db->query("UPDATE cluster_tb SET CLUSTER_ST = $cluster_st	WHERE CLUSTER_CD = $cluster_cd");
	$db->query("UPDATE candidate_tb SET CANDIDATE_ST = $cluster_st	WHERE CLUSTER_CD = $cluster_cd");

	$db->query("INSERT INTO cluster_history_tb
				(CLUSTER_CD, OPERATION, USER_CD, REGISTER_DT)
				VALUES ($cluster_cd, $cluster_st, $_SESSION[user_cd], now())");

	$col_nm1="STATUS_REGISTER_".$cluster_st;
	$col_nm2="STATUS_DT_".$cluster_st;
	$db->query("UPDATE cluster_work_tb
				SET  $col_nm1 = $_SESSION[user_cd], $col_nm2 = now()
				WHERE CLUSTER_CD = $cluster_cd ");

	$db->query("INSERT INTO candidate_history_tb
				(CANDIDATE_CD, OPERATION, USER_CD, REGISTER_DT)
        		SELECT CANDIDATE_CD,$cluster_st,$_SESSION[user_cd],now() FROM candidate_tb where CLUSTER_CD = $cluster_cd");

	$db->close();

	return $cluster_cd.",".$cluster_st;

}

?>
